<!doctype html>
<html>
  <head>
    <title>Notifications: Calling openWindow() in a Service Worker should not crash.</title>
    <script src="../resources/testharness.js"></script>
    <script src="../resources/testharnessreport.js"></script>
    <script src="../serviceworker/resources/test-helpers.js"></script>
    <script src="resources/test-helpers.js"></script>
  </head>
  <body>
    <script>
      // Tests that calling openWindow() in a Service Worker in response to a
      // notificationclick event does not cause the browser to crash.

      async_test(function(test) {
          var scope = 'resources/scope/' + location.pathname;
          var script = 'instrumentation-service-worker.js';
          var port;

          testRunner.setPermission('notifications', 'granted', location.origin, location.origin);
          getActiveServiceWorkerWithMessagePort(test, script, scope).then(function(info) {
              port = info.port;

              // (1) Display a persistent notification with an OPENWINDOW trigger.
              return info.registration.showNotification(scope, {
                  body: 'ACTION:OPENWINDOW'
              });
          }).then(function() {
              // (2) Simulate a click on the notification, invokes the Service Worker event.
              return simulateNotificationClick(scope, -1 /* action_index */, port);
          }).then(function(data) {
              // (3) Confirm that the service worker successfully handled the notificationclick
              // event for the correct notification.
              assert_equals(data.notification.title, scope);

              test.done();
          }).catch(unreached_rejection(test));

      }, 'Opening a window in a Service Worker should not trigger a DCHECK.');
    </script>
  </body>
</html>
